import time
import re
from lib2to3.pgen2 import driver

from selenium.webdriver.chrome import webdriver

from Page.UserPage.ErpLoginPage import ErpLoginPage
from selenium.webdriver.common.by import By as by
import random


class CreateNewVIP(ErpLoginPage):
    """表示层"""

    # 零售管理
    retail_management = (by.XPATH,'(//div[@class="ant-menu-submenu-title"])[1]')

    # 零售出库
    retail_delivery = (by.XPATH,'//a[@target="零售出库"]')

    # 点击引导关闭
    skip_button = (by.CSS_SELECTOR, '.introjs-skipbutton')

    # 关闭欢迎
    skip_welcome = (by.CSS_SELECTOR, '.ant-notification-close-x .ant-notification-close-icon svg')

    # 新增按钮
    new_order = (by.CSS_SELECTOR,'.table-operator .ant-btn-primary')

    # 随机会员号
    vip_num = ''
    for i in range(10):
        vip_num += str(random.randint(0, 9))

    # 会员卡号点击
    click_vip = (by.CSS_SELECTOR,'#organId')
    # 会员卡号输入框
    input_vip= (by.CSS_SELECTOR,'#organId .ant-select-search__field')

    # 新增会员按钮
    new_vip = (by.XPATH,'//div[@style="padding: 4px 8px; cursor: pointer;"]')
        # 控制台输入：setTimeout(function(){debugger},5000) 进入debug模式

    # 新增会员卡号输入框
    select_new_vip = (by.XPATH,'//input[@placeholder="请输入会员卡号"]')

    # 保存按钮
    save = (by.XPATH,'(//div[@class="ant-modal-footer"])[1]/div/button[@class="ant-btn ant-btn-primary"]')

    # 选择商品
    select_goods = (by.XPATH,'//span[@class="ant-input-group ant-input-group-compact"]/button')

    # 选择最新商品
    new_product = (by.CSS_SELECTOR, 'tbody.ant-table-tbody:nth-child(3)>tr.ant-table-row:nth-of-type(1)>td.ant-table-selection-column')

    # 确定按钮
    determine = (by.XPATH,'(//button[@class="ant-btn ant-btn-primary"])[7]')

    # 关闭按钮
    close_window = (by.XPATH, '(//button[@class="ant-modal-close"])[2]')


    # 保存并审核
    save_check = (by.XPATH,'(//button[@class="ant-btn"])[12]')

    # 查看小米手环库存，只获取了文本
    data_num = (by.CSS_SELECTOR, 'tbody.ant-table-tbody:nth-child(3)>tr:nth-of-type(1)>td:nth-of-type(10)')

    """操作层"""

    # 打开新增页面
    def open_new_vip(self):
        self.click(self.retail_management)
        time.sleep(1)
        self.click(self.retail_delivery)
        self.click(self.new_order)
        self.click(self.skip_button)
        time.sleep(1)


    # 输入不存在的会员并新增会员
    def add_new_vip(self):
        self.click(self.close_window)       # 第二次查看的关闭窗口好像不一样，这里直接放在新增会员的上面了
        time.sleep(2)
        self.js_click(self.click_vip)   # 为啥这里要用js点击
        self.send_keys(self.input_vip,self.vip_num)
        self.js_click(self.new_vip)     # 为啥这里要用js点击
        self.send_keys(self.select_new_vip,self.vip_num)
        self.click(self.save)
        time.sleep(2)


    # 选择小米手环并出库
    def delivery_goods(self):
        time.sleep(2)
        self.js_click(self.select_goods)
        time.sleep(1)
        self.js_click(self.new_product)
        self.click(self.determine)
        time.sleep(1)
        self.click(self.save_check)
        time.sleep(2)


    # 查看库存数量
    def get_inventory(self):
        self.js_click(self.select_goods)  # 为啥这里要用js点击
        # 拿到的是空值
        text = self.get_element_text(self.data_num)
        time.sleep(2)
        num = int(text)
        return num





    """业务层"""
if __name__ == '__main__':
    cnv = CreateNewVIP()

    cnv.common_login()
    cnv.open_new_vip()
    num1 = cnv.get_inventory()  # 获取出库前的库存量
    cnv.add_new_vip()
    cnv.delivery_goods()
    cnv.click(cnv.new_order)  # 获取出库后的库存量
    num2 = cnv.get_inventory()
    assert num1 - num2 == 1
    print(num1,num2)




